<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <button data-href="/eat">吃饭</button>
    <button data-href="/sleep">睡觉</button>
    <button data-href="/play">打豆豆</button>
    <div class="content"></div>
    <script>
      let btn = document.querySelectorAll('button')
      let content = document.querySelector('.content')
      let historyRoute = [
        { path: '/eat', content: '吃饭' },
        { path: '/sleep', content: '睡觉' },
        { path: '/play', content: '打豆豆' },
      ]
      btn.forEach((item) => {
        item.onclick = function () {
          // 向搜索框添加点击按钮的地址
          history.pushState(null, null, this.dataset.href)
          // 但是history不存在hash这种可以监视改变的函数,所以要在点击时实现数据替换
          historyRoute.forEach((item) => {
            if (item.path === this.dataset.href) {
              content.textContent = item.content
            }
          })
        }
      })
    </script>
  </body>
</html>
